//Usage ---
//	Mandatory parameters in show function: bgImage, buttonNormalImagesArray & buttonRectArray
//	var promptView = new GAAlertViewPanel();
//	promptView.show(PURCHASE_MSG.replace('PRICE', price.getDescription()) , "b03060", 13, null, './Content/UI/Alert/alert-panel.png', [ (this.purchaseButtonCallback).bind(this, model, price), (this.purchaseButtonCallbackCancel).bind(this)], ['./Content/UI/Alert/alert-normal.png', './Content/UI/Alert/alert-normal.png'], ['./Content/UI/Alert/alert-selected.png', './Content/UI/Alert/alert-selected.png'], ['OK', 'Cancel'], null, [[130, 180, 100, 50], [250, 180, 100, 50]], null);

exports.GAAlertViewPanel = UI.View.subclass(
{  
	classname: 'GAAlertViewPanel',
	
    //initialize: function() {   },
    
    show: function(message, messageFontColor, messageFontSize, rectForMessage, bgImage, callbackArray, buttonNormalImagesArray, buttonSelectedImagesArray, buttonTextArray, viewSize, buttonRectArray, buttonFontColorArray, btngradient, fontFamily, btnTextSelectedColor){
       	
		if(bgImage == null || buttonRectArray == null) {
	    	console.log("GAAlertViewPanel: Given parameters are not correct!");
			this.destroy();
			return;
	    }
	
		this.callbacks = callbackArray;
    	var width = GACommonUtils.getScreenWidth(); // change required
    	var height = GACommonUtils.getScreenHeight();  // change required
        NGWindow.document.addChild(this);
        var mainRect = new UI.ViewGeometry.Rect(0, 0, width, height);
        this.setFrame(mainRect.array());
        
         //adding background
        var pW = viewSize == null ? width/2+70 : viewSize.getWidth();
        var pH = viewSize == null ? height/2 : viewSize.getHeight();
        var promptRect = new UI.ViewGeometry.Rect(Math.abs(pW/2-width/2), Math.abs(pH/2-height/2), pW, pH);
        this._promptView = UIFactory.getView(promptRect.array(), null, null, null, UI.ViewGeometry.FitModes.Stretch);
		var uiImage=UIFactory.getImage([0,0,pW,pH], bgImage, null, null, null);
		this._promptView.addChild(uiImage);
        this.addChild(this._promptView);
        
        //adding buttons
        var totalButtons = buttonRectArray != null ? buttonRectArray.length : 0;
        for(var i=0; i<totalButtons; i++) {
        	if(i>=buttonRectArray.length) {
        		console.log("GAAlertViewPanel: Given parameters are not correct!");
        		this.destroy();
        		return;
        	}
        	var btnText = (buttonTextArray!=null && i<buttonTextArray.length) ? buttonTextArray[i] : null;
        	var btnTextFont = (buttonFontColorArray != null && i<buttonFontColorArray.length) ? buttonFontColorArray[i] : 'FFFFFF';
        	var btnImg = null;
        	if(buttonNormalImagesArray != null) {
        		btnImg=buttonNormalImagesArray[i];
        	}
        	var btn = UIFactory.getButton(buttonRectArray[i], btnImg,this._promptView,null,btnText,btnTextFont);
        	if(buttonSelectedImagesArray != null && i<buttonSelectedImagesArray.length) { btn.setImage(buttonSelectedImagesArray[i],UI.State.Pressed); }
        	btn.controller = this;
        	btn.index = i;
        	btn.onclick = this.callback;
        	if(btngradient){
        		btn.setGradient(btngradient[0],UI.State.Normal);
          		btn.setGradient(btngradient[1],UI.State.Pressed);
          		btn.setGradient(btngradient[2],UI.State.Disabled);
        	}
            if(fontFamily){
            	btn.setTextFont(fontFamily);
            }
            if(btnTextSelectedColor) {
				btn.setTextColor(btnTextSelectedColor,UI.State.Pressed);
			}
        	this._promptView.addChild(btn);
        }
            
        //adding Text Area
        rectForMessage = rectForMessage == null ? [125, 70, width/2.5, height/4.5] : rectForMessage;
        messageFontColor = messageFontColor == null ? "b03060" : messageFontColor;
        messageFontSize = messageFontSize == null ? 14 : messageFontSize;
        var alertText = UIFactory.getTextArea(rectForMessage, message, messageFontSize, [0.0, 0.5], messageFontColor);
		this._promptView.addChild(alertText);
    },
    callback: function() {
    	if(this.controller.callbacks != null && this.index < this.controller.callbacks.length && this.controller.callbacks[this.index] != null) {
    		this.controller.callbacks[this.index]();
    	}
        this.controller.destroy();
    },
    destroy: function() {
		if(this._promptView != 'undefined' && this._promptView != null) {
			var children = this._promptView.getChildren();
			var length = children.length;
			for(var i = length-1; i >= 0; i--) {
					children[i].destroy();
					children[i] = null;
			}
			this._promptView.destroy();
			this._promptView = null;
		}
	}
});
